import java.util.ArrayList;


public class AttributeHandler {
  private final String FINISH = "FINISH";
  private final String NOTFINISH = "NOTFINISH";
  private final String NOTHING = "";
  private final String SPLITTER= ",";
  private final String STRING_CSV_FORMAT = "%1$s" + "," + "%2$s" + "," + "%3$s" + "," + "%4$s" + "," + "%5$s";
  
  private String taskTitle;
  private String taskTime;
  private String taskTag;
  private String taskIsFinish;
  private int taskUrgencyLevel;
  
  private String stringToBeReturned;
  private ArrayList<String> stringListToBeReturned;
  
  private Task taskToBeReturned;
  private ArrayList<Task> taskListToBeReturned;

//Public--------------------------------------------------------------------------------  
  public String convertTaskToString(Task dataBuffer) {
    if(dataBuffer != null) {
      extractAttributesFromDataBuffer(dataBuffer);
      stringToBeReturned = constructStringFormat();
    } else {
      stringToBeReturned = NOTHING;
    }
    return stringToBeReturned;
  }
  
  public ArrayList<String> convertTaskToString(ArrayList<Task> dataBufferList) {
    stringListToBeReturned = new ArrayList<String>();
    if(dataBufferList == null) {
      return stringListToBeReturned;
    }
    for(int i = 0; i < dataBufferList.size(); i++) {
      extractAttributesFromDataBuffer(dataBufferList.get(i));
      stringListToBeReturned.add(constructStringFormat());
    }
    return stringListToBeReturned;
  }
  
  public Task convertStringToTask(String dataBuffer) {
    taskToBeReturned = new Task();
    if(!dataBuffer.equals(NOTHING)) {
      taskToBeReturned = convertStringToObject(dataBuffer);
    }
    return taskToBeReturned;
  }
  
  public ArrayList<Task> convertStringToTask(ArrayList<String> dataBufferList) {
    taskListToBeReturned = new ArrayList<Task>();
    if(dataBufferList == null) {
      return taskListToBeReturned;
    }
    for(int i = 0; i < dataBufferList.size(); i++) {
      taskListToBeReturned.add(convertStringToObject(dataBufferList.get(i)));
    }
    return taskListToBeReturned;
  }
  //-------------------------------------------------------------------------------------
  
  private Task convertStringToObject(String taskString) {
    String[] taskAttributes = taskString.split(SPLITTER);
    ArrayList<String> taskContructor = new ArrayList<String>();
    taskContructor.add(NOTHING);
    for (int i = 0; i < taskAttributes.length; i++) {
      taskContructor.add(taskAttributes[i]);
    }
    
    Task returnTask = new Task(taskContructor);
    if(taskAttributes[taskAttributes.length-1].equals(FINISH)) {
      returnTask.finishATask();
    }
    return returnTask;
  }
  
  
  private void extractAttributesFromDataBuffer(Task dataBuffer) {
    extractDateFromDataBuffer(dataBuffer);
    extractTitleFromDataBuffer(dataBuffer);
    extractTagFromDataBuffer(dataBuffer);
    extractUrgencyLevelFromDataBuffer(dataBuffer);
    extractIsFinishFromDataBuffer(dataBuffer);   
  }
  
  private void extractIsFinishFromDataBuffer(Task dataBuffer) {
    if (dataBuffer.isThisTaskFinished()) {
      taskIsFinish = FINISH;
    } else {
      taskIsFinish = NOTFINISH;
    }
  }
  
  private void extractUrgencyLevelFromDataBuffer(Task dataBuffer) {
    taskUrgencyLevel = dataBuffer.getUrgencyLevel();
  }
  
  private void extractTagFromDataBuffer(Task dataBuffer) {
    taskTag = dataBuffer.getTag();
  }

  private void extractDateFromDataBuffer(Task dataBuffer) {
    taskTime = dataBuffer.getTime();
  }

  private void extractTitleFromDataBuffer(Task dataBuffer) {
    taskTitle = dataBuffer.getTitle();
  }
  
  private String constructStringFormat() {
    String taskString = String.format(STRING_CSV_FORMAT, taskTitle, taskTime, taskTag, taskUrgencyLevel, taskIsFinish);
    return taskString;
  }
}
